package bootstrap

import (
	"context"

	"github.com/alist-org/alist/v3/internal/conf"
	"github.com/alist-org/alist/v3/internal/db"
	"github.com/alist-org/alist/v3/internal/model"
	"github.com/alist-org/alist/v3/internal/op"
	"github.com/alist-org/alist/v3/pkg/utils"
)

func LoadStorages() {
	// 获取所有已启用的存储配置
	storages, err := db.GetEnabledStorages()
	if err != nil {
		utils.Log.Fatalf("获取已启用存储失败: %+v", err)
	}
	
	// 异步加载所有存储配置
	go func(storages []model.Storage) {
		for i := range storages {
			// 加载单个存储
			err := op.LoadStorage(context.Background(), storages[i])
			if err != nil {
				utils.Log.Errorf("加载存储失败: %+v", err)
			} else {
				utils.Log.Infof("成功加载存储: [%s], 驱动类型: [%s], 排序: [%d]",
					storages[i].MountPath, storages[i].Driver, storages[i].Order)
			}
		}
		// 标记存储加载完成
		conf.StoragesLoaded = true
	}(storages)
}
